package icu.xblogs.report.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Frank KONG
 * @version 1.0
 * @date 2020/11/17 9:40
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    private HandlerInterceptor identityInterceptor;
    private HandlerInterceptor managerInterceptor;

    @Autowired
    @Qualifier("identityInterceptor")
    public void setIdentityInterceptor(HandlerInterceptor identityInterceptor) {
        this.identityInterceptor = identityInterceptor;
    }

    @Autowired
    @Qualifier("managerInterceptor")
    public void setManagerInterceptor(HandlerInterceptor managerInterceptor) {
        this.managerInterceptor = managerInterceptor;
    }

    private static final String[] EMPLOYEE_PATHS = {"/page/employee.html", "/employee/**"};
    private static final String[] MANAGER_PATHS = {"/page/manager.html", "/manager/**"};

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(identityInterceptor)
                .addPathPatterns(EMPLOYEE_PATHS);

        registry.addInterceptor(managerInterceptor)
                .addPathPatterns(MANAGER_PATHS);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("/index");
    }
}
